package model;

import java.util.Vector;

public class Position {
	public int x, y, xTo, yTo, anchor;

	public Position() {
		this.x = 0;
		this.y = 0;
	}

	public Position(int x, int y, int anchor) {
		this.x = x;
		this.y = y;
		this.anchor = anchor;
	}

	public Position(int x, int y) {
		this.x=x;
		this.y=y;
	}
	
	public void setPosition(int x, int y){
		this.x = x;
		this.y = y;
	}
	
	public void copy(Position pos){
		this.x = pos.x;
		this.y = pos.y;
		this.xTo = pos.xTo;
		this.yTo = pos.yTo;
		this.anchor = pos.anchor;
	}
	
public void getPositionAnimal(Position nextPos, Vector vectorPosition){
		
		int x1 = this.x;
		int x2 = nextPos.x;
		int y1 = this.y;
		int y2 = nextPos.y;
		int deltax = Math.abs(x2 - x1);    	// The difference between the x's
		int deltay = Math.abs(y2 - y1);    	// The difference between the y's
		int x = x1;                   	// Start x off at the first pixel
		int y = y1;                   	// Start y off at the first pixel
		int xinc1,xinc2,yinc1,yinc2;
		if (x2 >= x1)             	// The x-values are increasing
		{
		  xinc1 = 1;
		  xinc2 = 1;
		}
		else                      	// The x-values are decreasing
		{
		  xinc1 = -1;
		  xinc2 = -1;
		}

		if (y2 >= y1)             	// The y-values are increasing
		{
		  yinc1 = 1;
		  yinc2 = 1;
		}
		else                      	// The y-values are decreasing
		{
			yinc1 = -1;
			yinc2 = -1;
		}
		int num,den,numadd,numpixels;
		if (deltax >= deltay)     	// There is at least one x-value for every y-value
		{
			xinc1 = 0;              	// Don't change the x when numerator >= denominator
			yinc2 = 0;              	// Don't change the y for every iteration
			den = deltax;
			num = deltax / 2;
			numadd = deltay;
	  		numpixels = deltax;     	// There are more x-values than y-values
		}
		else                      	// There is at least one y-value for every x-value
		{
			xinc2 = 0;              	// Don't change the x for every iteration
			yinc1 = 0;              	// Don't change the y when numerator >= denominator
			den = deltay;
			num = deltay / 2;
			numadd = deltax;
			numpixels = deltay;     	// There are more y-values than x-values
		}

		for (int curpixel = 0; curpixel <= numpixels; curpixel++)
		{
		 // PutPixel(x, y);         	// Draw the current pixel
			//g.drawLine(x, y, x, y);
			Position tmpPosition = new Position();
			tmpPosition.setPosition(x,y);
			vectorPosition.addElement(tmpPosition);
			tmpPosition = null;
			num += numadd;          	// Increase the numerator by the top of the fraction
			if (num >= den)         	// Check if numerator >= denominator
			{
				num -= den;           	// Calculate the new numerator value
				x += xinc1;           	// Change the x as appropriate
				y += yinc1;           	// Change the y as appropriate
			}
			x += xinc2;             	// Change the x as appropriate
			y += yinc2;             	// Change the y as appropriate
		}	
	}
		

	void draw(Position nextPos, Vector vectorPosition)
	{
		int x1 = this.x;
		int x2 = nextPos.x;
		int y1 = this.y;
		int y2 = nextPos.y;
		
		float x,y,xinc,yinc,dx,dy;
		int k;
		int step;
		dx=x2-x1;
		dy=y2-y1;
		if(Math.abs(dx)>Math.abs(dy))
			step=(int) Math.abs(dx);
		else
			step=(int) Math.abs(dy);
		xinc=dx/step;
		yinc=dy/step;
		x=x1;
		y=y1;
		
		//putpixel(x,y,1);
		Position tmpPosition = new Position();
		tmpPosition.setPosition((int)x,(int)y);
		vectorPosition.addElement(tmpPosition);
		tmpPosition = null;
		
		for(k=1;k<=step;k++)
		{
			x=x+xinc;
			y=y+yinc;
			//putpixel(x,y,2);
			tmpPosition = new Position();
			tmpPosition.setPosition((int)x,(int)y);
			vectorPosition.addElement(tmpPosition);
			tmpPosition = null;
		}
	}
}
